import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/train/train_degree_of_statisfaction_page_item.dart';
import 'package:flutter_learn_vi/src/page/train/train_user_check_recoder_my_remark_add_page.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import 'controller/train_degree_of_statisfaction_controller.dart';

///教师的培训
class TrainDegreeOfStatisFactionPage extends StatefulWidget {
  final String tag;

  final int trainId;

  const TrainDegreeOfStatisFactionPage(
      {Key key, @required this.trainId, @required this.tag})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TrainDegreeOfStatisFactionPageState();
  }
}

class _TrainDegreeOfStatisFactionPageState
    extends State<TrainDegreeOfStatisFactionPage>
    with SingleTickerProviderStateMixin {
  TrainDegreeOfStatisfactionController _checkRecoderController;

  @override
  void initState() {
    super.initState();
    _checkRecoderController = Get.put(
      TrainDegreeOfStatisfactionController(widget.trainId),
      tag: widget.tag,
    );

    Future.delayed(Duration.zero, () async {
      await _checkRecoderController.requestUserCheckRecoderFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("培训管理满意度测评"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.tag,
        builder: (TrainDegreeOfStatisfactionController controller) {
          return buildAnimatedSwitcher(context);
        },
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildMyScoreHisIntegralWidget());
  }

  buildMyScoreHisIntegralWidget() {
    bool _isLoading = _checkRecoderController.getIsLoading();
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }

    return RefreshIndicator(
      onRefresh: () async {
        await _checkRecoderController.requestUserCheckRecoderFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: SingleChildScrollView(
        child: buildListView(),
      ),
    );
  }

  buildListView() {
    TrainDegreeOfStatisfactionEntity _checkRecoderEntity =
        _checkRecoderController.getTrainUserCheckRecoderEntity();

    return Column(
      children: [
        Container(
          color: Colors.white,
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12),
          child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "说明:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      Expanded(
                        child: SizedBox(width: 10),
                      ),
                    ],
                  ),
                  SizedBox(height: 4),
                  Text("${_checkRecoderController.tips}")
                ],
              )),
        ),
        buildBottomStartButton(),
        buildStateInfoWidget(),
      ],
    );
  }

  buildStateInfoWidget() {
    TrainDegreeOfStatisfactionEntity _checkRecoderEntity =
        _checkRecoderController.getTrainUserCheckRecoderEntity();
    if (_checkRecoderEntity == null) {
      return SizedBox();
    }
    if (_checkRecoderEntity.statues == 0) {
      return SizedBox();
    }
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          color: Colors.white,
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12),
          padding: EdgeInsets.all(12),
          child: Row(
            children: [
              Expanded(
                child: Text(
                  "我的评分是:",
                  style: TextStyle(
                      fontWeight: FontWeight.w500, color: Colors.black),
                ),
              ),
              Text(
                "${_checkRecoderEntity.attendanceNumber}分",
                style:
                    TextStyle(fontWeight: FontWeight.w500, color: Colors.black),
              ),
            ],
          ),
        ),
        Container(
          color: Colors.white,
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12),
          child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "对此次培训的总体评价:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      Expanded(
                        child: SizedBox(width: 10),
                      ),
                      _checkRecoderEntity.shoTeacherDegree
                          ? TextButton(
                              onPressed: () async {
                                await Get.to(
                                  () => TrainUserCheckRecoderAddPage(
                                      recoderId: _checkRecoderEntity.id,
                                      remark: _checkRecoderEntity.trainRemark,
                                      trainUserCheckEnum: TrainUserCheckEnum
                                          .degreeOfTrainRemark),
                                );
                                _checkRecoderController
                                    .requestUserCheckRecoderFunction(
                                        click: true);
                              },
                              child: Text("编辑"))
                          : SizedBox(),
                    ],
                  ),
                  SizedBox(height: 4),
                  Text("${_checkRecoderEntity.trainRemark ?? "无"}")
                ],
              )),
        ),
        Container(
          color: Colors.white,
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12),
          child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "我的意见和建议:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      Expanded(
                        child: SizedBox(width: 10),
                      ),
                      TextButton(
                          onPressed: () async {
                            await Get.to(
                              () => TrainUserCheckRecoderAddPage(
                                recoderId: _checkRecoderEntity.id,
                                remark: _checkRecoderEntity.myRemark,
                                trainUserCheckEnum:
                                    TrainUserCheckEnum.degreeOfMyRemark,
                              ),
                            );
                            _checkRecoderController
                                .requestUserCheckRecoderFunction(click: true);
                          },
                          child: Text("编辑"))
                    ],
                  ),
                  SizedBox(height: 4),
                  Text("${_checkRecoderEntity.myRemark ?? "无"}")
                ],
              )),
        ),
      ],
    );
  }

  buildBottomStartButton() {
    TrainDegreeOfStatisfactionEntity _checkRecoderEntity =
        _checkRecoderController.getTrainUserCheckRecoderEntity();
    if (_checkRecoderEntity == null) {
      return SizedBox();
    }

    if (_checkRecoderEntity.statues != 0) {
      return SizedBox();
    }

    if (!_checkRecoderEntity.shoTeacherDegree) {
      String message = "";
      if (_checkRecoderEntity.shoTeacherDegreeMessage != null) {
        message = _checkRecoderEntity.shoTeacherDegreeMessage;
      }
      return Container(
        child: Text(message),
      );
    }

    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
              onPressed: () async {
                await Get.to(() => TrainDegreeOfStatisFactionItemPage(
                      trainId: widget.trainId,
                    ));
                _checkRecoderController.requestUserCheckRecoderFunction(
                    click: true);
              },
              child: Text("开始测评"))
        ],
      ),
    );
  }
}
